package com.wd.mine.fragment

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.SPUtils.*
import com.bumptech.glide.Glide
import com.wd.common.base.BaseFragment
import com.wd.common.bean.UserInfoBean
import com.wd.mine.activity.AbroadMapSwitchActivity
import com.wd.mine.activity.CollectionAActivity
import com.wd.mine.activity.CollectionActivity
//import com.wd.mine.activity.ElectricActivity
import com.wd.mine.activity.MeFollowActivity
import com.wd.mine.activity.MeLoginActivity
import com.wd.mine.databinding.FragmentMineBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MineFragment : BaseFragment<FragmentMineBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = FragmentMineBinding.inflate(layoutInflater)

    override fun initData() {
        val uid = SPUtils.getInstance().getInt("userId", 0)
        //点击头像进入登录页
        binding.userHeadpic.setOnClickListener {
            if(uid != 0){
                context?.startActivity(Intent(context, MeFollowActivity::class.java))
            }
        }
        //我的收藏
        binding.collection.setOnClickListener {
            if(uid != 0){
                context?.startActivity(Intent(context, CollectionAActivity::class.java))
            }
        }
        //我的关注
        binding.follow.setOnClickListener {
            if(uid != 0){
                context?.startActivity(Intent(context, MeFollowActivity::class.java))
            }else{
                context?.startActivity(Intent(context, MeLoginActivity::class.java))
            }
        }
        //找桩充电
        binding.ownerElectric.setOnClickListener {
            context?.startActivity(Intent(context, AbroadMapSwitchActivity::class.java))
        }
        val string = getInstance().getString("headPic")
        if(string != ""){
            Glide.with(this)
                .load(string)
                .circleCrop()
                .into(binding.userHeadpic)
        }
    }

    @Subscribe(sticky = true,threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: UserInfoBean) {
        Glide.with(this)
            .load(event.result?.headPic)
            .into(binding.userHeadpic)
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }
    override fun onDestroyView() {
        super.onDestroyView()

    }

}